/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package heart_0812025;

import java.util.Random;

/**
 *
 * @author NguyenKhoi
 */
public class GameManager
{
    public static final int NUM_CARD = 52;
    protected Player player1, player2, player3, player4;
    public GameManager()
    {
        player1 = new Player();
        player2 = new Player();
        player3 = new Player();
        player4 = new Player();
    }
    public void startNewGame()
    {
        ChiaBai();
    }

    public Player GetPlayer(int playerId)
    {
        Player player = null;
        switch(playerId)
        {
            case 1:
                player = player1; break;
            case 2:
                player = player2; break;
            case 3:
                player = player3; break;
            case 4:
                player = player4; break;
            default:
                player = null;
        }
        return player;
    }

    public void ChiaBai()
    {
        int[] listCardId = new int[NUM_CARD];
        for(int i = 0; i<NUM_CARD; i++)
        {
            int id = i+1;
            listCardId[i] = id;
        }

        //Hoan doi Id cac la bai

        Random rand = new Random();
        for(int i = 0; i<NUM_CARD; i++)
        {
            int j = rand.nextInt(NUM_CARD);
            if(i != j)
            {
                int tempId = listCardId[i];
                listCardId[i] = listCardId[j];
                listCardId[j] = tempId;
            }
        }
        //Tao list cac la bai
        Card[] listCard = new Card[NUM_CARD];
        for(int i = 0; i<NUM_CARD; i++)
        {
            int tempId = listCardId[i];
            listCard[i] = new Card(tempId);
        }

        //Chia bai cho cac player
        //Player1 card 0 -> 12
        //Player2 card 13 -> 25
        //Player3 card 26 -> 38
        //Player4 card 39 -> 51
        for(int i = 0; i<13; i++)
        {
            player1.AddNewCard(listCard[i + 13*0]);
            player2.AddNewCard(listCard[i + 13*1]);
            player3.AddNewCard(listCard[i + 13*2]);
            player4.AddNewCard(listCard[i + 13*3]);
        }
    }

}
